package com.example.dllo.my36kr.ui.fragment;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.model.MineBean;
import com.example.dllo.my36kr.ui.activity.LoginActivity;
import com.example.dllo.my36kr.ui.activity.sonactivity.CollectActcvity;
import com.example.dllo.my36kr.ui.adapter.MineAdapter;
import java.util.ArrayList;
import cn.bmob.v3.BmobUser;


/**我的界面
 * Created by CPC_272 on 16/8/15.
 */
public class MineFragment  extends AbsBaseFragment implements AdapterView.OnItemClickListener, View.OnClickListener {
    private ListView listView;
    private ArrayList<MineBean>datas;
    private ImageView mine_heard,mine_return;
    private TextView mine_open;
    private BmobUser bmobUser;

    @Override
    protected int setLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        listView =byView(R.id.ls_mine);
        mine_open =byView(R.id.mine_open);
          mine_heard =byView(R.id.mine_heard);
        mine_return = byView(R.id.mine_return);
    }

    @Override
    protected void initData() {
        bmobUser = BmobUser.getCurrentUser();
        if (bmobUser != null) {
            //这里面有数据就执行了这个方法
            mine_open.setText("我是敞篷车");
            //这个是JPG格式的照片有可能会报错
            mine_heard.setImageResource(R.mipmap.ic_launcher);
        }else{
            mine_open.setText("未登录");
            //这个是JPG格式的照片有可能会报错
            mine_heard.setImageResource(R.mipmap.common_avatar);
        }
            listView.setOnItemClickListener(this);
            mine_open.setOnClickListener(this);
            mine_return.setOnClickListener(this);
            mine_heard.setOnClickListener(this); 
    addDatas();
    }
    //为LISTVIEW添加数据
    private void addDatas() {
        datas = new ArrayList<>();
        datas.add(new MineBean(""));
        datas.add(new MineBean(R.mipmap.minellll, " 我的订单"));
        datas.add(new MineBean(""));
        datas.add(new MineBean( R.mipmap.mineooo,  " 账号信息"));
        datas.add(new MineBean( R.mipmap.mine_icon_authentication,"跟投人认证"));
        datas.add(new MineBean(""));
        datas.add(new MineBean( R.mipmap.mine_icon_authentication,"我收藏的文章"));
        datas.add(new MineBean( R.mipmap.mine_icon_company,"我的投资公司"));
        datas.add(new MineBean( R.mipmap.mine_icon_coupon,"我的投资券"));
        datas.add(new MineBean( R.mipmap.mine_icon_understand,"了解股权投资"));
        datas.add(new MineBean(""));
        datas.add(new MineBean( R.mipmap.mine_icon_hotline,"客服热线"));
        datas.add(new MineBean(""));
        datas.add(new MineBean("                            退出登录     "));
        datas.add(new MineBean(""));
        MineAdapter mineAdapter = new MineAdapter(getContext());
        mineAdapter.setData(datas);
        listView.setAdapter(mineAdapter);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position){
            case 6:
          Intent intent1=new Intent(context, CollectActcvity.class);
                context.startActivity(intent1);
                break;
            case 11:
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + "400-995-3636");
                    intent.setData(data);
                    startActivity(intent);
                break;
            case 13:
                //退出登录
                    BmobUser.logOut();
                initData();
                Toast.makeText(context, "退出登入", Toast.LENGTH_SHORT).show();
                break;

        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mine_open:
                if (bmobUser != null) {
                    mine_open.setText("我是敞篷车");
                    //这个是JPG格式的照片有可能会报错
                    mine_heard.setImageResource(R.mipmap.ic_launcher);
                }else{
                    Intent intent = new Intent(context, LoginActivity.class);
                    context.startActivity(intent);
                }
                break;
            case R.id.mine_heard://头像
                if (bmobUser != null) {
                    mine_open.setText("我是敞篷车");
                    //这个是JPG格式的照片有可能会报错
                    mine_heard.setImageResource(R.mipmap.ic_launcher);
                }else {
                    Intent intent1 = new Intent(context, LoginActivity.class);
                    context.startActivity(intent1);
                }
                break;
            case R.id.mine_return: //>按钮
                if (bmobUser != null) {
                    mine_open.setText("我是敞篷车");
                    //这个是JPG格式的照片有可能会报错
                    mine_heard.setImageResource(R.mipmap.ic_launcher);
                }else {
                    Intent intent2 = new Intent(context, LoginActivity.class);
                    context.startActivity(intent2);
                }
                break;
        }
    }
}
